<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          size="small"
          @click="view(scope.row, scope.index)"
        >查看
        </el-button>
        <el-button
          type="text"
          size="small"
          @click="questionnaire(scope.row, scope.index)"
        >问卷详情
        </el-button>
        <el-button
          v-if="scope.row.examineState === AUDIT_STATUS.PENDING_AUDIT"
          type="text"
          size="small"
          @click="audit(scope.row, scope.index)"
        >审核
        </el-button>
      </template>
    </avue-crud>
    <auditPopup
      :id="id"
      v-model="auditVisible"
      @success="(val) => getList(page, val)"
    ></auditPopup>
    <detailPopup
      :id="id"
      v-model="detailVisible"
      @success="(val) => getList(page, val)"
    ></detailPopup>
    <questionnairePopup
      :id="id"
      v-model="questionnaireVisible"
      @success="(val) => getList(page, val)"
    ></questionnairePopup>
  </basic-container>
</template>

<script>
import { fetchList, getObj } from '@/api/core/senderApply';
import { tableOption } from '@/const/crud/customer/adopterApply';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
import auditPopup from './components/auditPopup.vue';
import detailPopup from './components/detailPopup.vue';
import questionnairePopup from './components/questionnairePopup.vue';
import { RESERVE_TYPE, AUDIT_STATUS } from '@/const/type';
export default {
  name: 'customerManage',
  components: { auditPopup, detailPopup, questionnairePopup },
  mixins: [crudMixins],
  data() {
    return {
      auditVisible: false,
      form: {},
      tableOption: tableOption(),
      userId: null,
      addVisible: false,
      detailVisible: false,
      id: null,
      questionnaireVisible: false,
      RESERVE_TYPE,
      AUDIT_STATUS,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {},
  methods: {
    view(row) {
      this.detailVisible = true;
      this.id = row.id;
    },
    questionnaire(row) {
      this.questionnaireVisible = true;
      this.id = row.id;
    },
    audit(row) {
      this.auditVisible = true;
      this.id = row.id;
    },
    add() {
      this.addVisible = true;
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      const timeObj = {};
      if (this.searchForm.applyTime && this.searchForm.applyTime.length) {
        timeObj.applyStartTime = this.searchForm.applyTime[0];
        timeObj.applyEndTime = this.searchForm.applyTime[1];
      }
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
            type: RESERVE_TYPE.ADOPT,
          },
          this.searchForm,
          timeObj
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    // 打开前的拦截
    async beforeOpen(done, type) {
      if (type !== 'add') {
        const res = await getObj(this.form.userId);
        this.form = { ...res.data.data };
      }
      this.form.avatar = this.form.avatar
        ? this.form.avatar
        : '/img/default-avatar.png';
      done();
    },
  },
};
</script>
